<template>
  <div class="mgt10">
    <el-form :model="query" label-position="top">
      <el-row :gutter="15">
        <el-col :span="4">
          <el-form-item label="订单号/渠道单号:">
            <el-input v-model="query.orderNumber"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="7">
          <el-row>
            <el-form-item label="付款时间:">
              <el-col :span="11">
                <el-date-picker v-model="query.paidOnFrom" type="date" placeholder="起始时间" />
              </el-col>
              <el-col :span="2" class="text-center">至</el-col>
              <el-col :span="11">
                <el-date-picker v-model="query.paidOnTo" type="date" placeholder="截止时间" />
              </el-col>
            </el-form-item>
          </el-row>
        </el-col>
        <el-col :span="8">
          <el-form-item label="信用卡:">
            <el-select v-model="query.creditCardId" multiple filterable :filter-method="loadCreditCards">
              <el-option v-for="cc in creditCards" :key="cc.id" :value="cc.id" 
                :label="shortOwnerName(cc)">
                <span>{{ cc.owner }}</span>
                <span class="tips">{{ cc.bank }}-{{cc.cardType}}</span>
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="" class="no-top-label">
            <el-button type="primary" icon="search" @click="loadPage(1)">检索</el-button>
            <el-button type="success" @click="loadPage(1)">导出</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-table :data="page.content" v-loading.body="loading" border show-summary 
      :summary-method="summaryTotalCost">
      <el-table-column prop="id" label="ID" width="80" align="center" />
      <el-table-column prop="paidOn" label="付款时间" width="175" />
      <el-table-column prop="orderNumber" label="订单号" />
      <el-table-column prop="channelOrderNumber" label="渠道单号" />
      <el-table-column prop="cost" label="付款方式" align="center">
        <template slot-scope="scope">
          {{scope.row.currency}}:{{scope.row.cost/100}} ({{PaymentMode[scope.row.paymentMode]}})
        </template>
      </el-table-column>
      <el-table-column label="信用卡" align="center">
        <template slot-scope="scope">
          <div>{{scope.row.creditCard.owner}}</div>
          <div>({{scope.row.creditCard.bank}}/{{scope.row.creditCard.cardType}})</div>
        </template>
      </el-table-column>
      <!--
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <router-link :to="'/dtb/details?dtbId=' + scope.row.id">
            <el-button type="primary" size="small">详情</el-button>
          </router-link>
        </template>
      </el-table-column>
      -->
    </el-table>

    <!-- pagination -->
    <el-pagination @current-change="showPage" :current-page.sync="page.page" 
      :page-size="page.pageSize" :total="page.totalElements" 
      layout="total, prev, pager, next, jumper" />
  </div>
</template>

<script>
import { PurchaseOrder, CreditCard } from '@/store/api'
import Pagination from '@/utils/pagination'
import * as Constants from '@/constants'
export default {
  data() {
    return {
      ...Constants,
      loading: true,
      balance: {},
      query: {
        status: '',
        paidOnFrom: new Date()
      },
      creditCards: [],
      page: {
        content: []
      }
    }
  },
  created() {
    this.loadCreditCards()
    this.loadPage(1)
  },
  methods: {
    ...Pagination,
    list(page, query) {
      this.loading = true
      return PurchaseOrder.list(query).then(data => {
        this.page = data
        this.loading = false
      })
    },
    loadCreditCards(keyword) {
      CreditCard.list({ keyword: keyword }).then(page => {
        this.creditCards = page.content
      })
    },
    shortOwnerName(cc) {
      let owner = cc.owner
      // let owner = cc.owner.substring(0, 3) + '**' + cc.owner.substring(cc.owner.length - 3)
      let cardType = '(' + cc.cardType + ')'
      return owner + cardType
    },
    summaryTotalCost(table) {
      let {columns, data} = table
      const summary = []
      columns.forEach((column, index) => {
        if (index === 0) {
          summary[index] = '总计'
        } else if (column.property === 'cost') {
          // const values = data.map(item => Number(item[column.property]))
          const sum = data.reduce((prev, curr) => {
            const value = Number(curr[column.property])
            return isNaN(value) ? prev : prev + value
          }, 0)
          summary[index] = sum / 100
        } else {
          summary[index] = null
        }
      })
      return summary
    }
  }
}
</script>

<style lang="scss" scoped>
.el-input,
.el-select {
  width: 100%;
}

.tips {
  float: right;
  color: #8492a6;
  font-size: 13px;
  margin-right: 25px;
}
</style>
